package cmw.com.beijingnew.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import cmw.com.beijingnew.R;
import cmw.com.beijingnew.SplashActivity;
import cmw.com.utils.CacheUtil;
import cmw.com.utils.DensityUtil;

import static android.content.ContentValues.TAG;

/**
 * 引导页Activity
 */
public class GuideActivity extends Activity {
    public static final String TAG = GuideActivity.class.getSimpleName();
    private ViewPager viewPager = null;
    private Button btnStart = null;
    private LinearLayout ll_groupDot = null;
    private List<ImageView> imageViewList = null;
    private ImageView iv_red_point = null;
    private int leftmax;
    private int dipWidth = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        btnStart = (Button) findViewById(R.id.btnStart);
        ll_groupDot = (LinearLayout) findViewById(R.id.ll_groupDot);
        iv_red_point= (ImageView) findViewById(R.id.iv_red_point);
        int[] ids = new int[]{R.drawable.activity_1,R.drawable.activity_2,R.drawable.activity_3};
        dipWidth = DensityUtil.dip2px(this, 10);
        imageViewList = new ArrayList<ImageView>();
        int i = 0;
        for(int id : ids){
            /**----- STEP 1 : 设置3张引导页图片到ViewPager中 ----**/
            ImageView imageView = new ImageView(this);
            imageView.setBackgroundResource(id);
            imageViewList.add(imageView);

            /**----- STEP 2 : 设置底部圆点到LineLayout中 ----**/
            //创建圆点
            ImageView point = new ImageView(this);
            //默认为灰色圆点
            point.setBackgroundResource(R.drawable.point_nomal);
            //设置圆点宽、高分别为10个像素
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dipWidth,dipWidth);
            if(i != 0){//第一个圆点不需要设置左间距
                params.leftMargin=10;
            }

            point.setLayoutParams(params);
            //将圆点添加到LineLayout中
            ll_groupDot.addView(point);
            i++;
        }

        viewPager.setAdapter(new MyPageAdapter());
        /*根据View的生命周期，当视图执行到onLayout或者onDraw的时候，视图的高和宽，边距都有了*/
        iv_red_point.getViewTreeObserver().addOnGlobalLayoutListener(new MyOnGlobalLayoutListner());
        /*添加页面滑动事件（通过该事件可以得到页面滑动的百分比）*/
        viewPager.addOnPageChangeListener(new MyOnPageChangeListener());

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1.记录曾经进入过的主页面
                CacheUtil.putBoolean(GuideActivity.this, SplashActivity.START_MAIN, true);

                //2.跳转到主页面
                Intent intent = new Intent(GuideActivity.this, MainActivity.class);
                startActivity(intent);

                //3.关闭当前页面
                finish();
            }
        });
    }

    /**
     * ViewPager 添加引导图片适配器
     */
    class MyPageAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return imageViewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = imageViewList.get(position);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    private class MyOnGlobalLayoutListner implements ViewTreeObserver.OnGlobalLayoutListener {
        @Override
        public void onGlobalLayout() {
            //removeGlobalOnLayoutListener 让当前事件只执行一次，如果不加此方法，则 onGlobalLayout() 会执行两次
            iv_red_point.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            //计算第一个圆点到第零个圆点的间距
            leftmax = ll_groupDot.getChildAt(1).getLeft() - ll_groupDot.getChildAt(0).getLeft();
            Log.i(TAG,"leftmax="+leftmax);
        }
    }

    /**
     * ViewPager 页面改边监听事件
     */
    private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        /**
         * 页面滑动事件
         * @param position  当前滑动页面的位置
         * @param positionOffset    页面滑动的百分比
         * @param positionOffsetPixels  滑动的像素
         */
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
           // Log.e(TAG,"position="+position+",positionOffset="+positionOffset+",positionOffsetPixels="+positionOffsetPixels);
           //两点间移动的距离 = 屏幕划动的百分比 * 间距
            int leftMargin = (int) (positionOffset * leftmax);
            //两点间滑动距离对应的坐标 = 原来的起始位置 + 两点间移动的距离
            leftMargin = (position * leftmax) + leftMargin;
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv_red_point.getLayoutParams();
            params.leftMargin = leftMargin;
            iv_red_point.setLayoutParams(params);
        }

        /**
         * 当页页被选中时，调用此方法
         * @param position  被选中页面的对应位置
         */
        @Override
        public void onPageSelected(int position) {
            if(position == imageViewList.size() - 1){//如果滑动到最后一个引导图片，就显示开始按钮
                btnStart.setVisibility(View.VISIBLE);
            }else{//如果滑动到非最后一个引导图片，就隐藏开始按钮
                btnStart.setVisibility(View.GONE);
            }
        }

        /**
         * 当ViewPager页面滑动状态发生变化的时候
         * @param state
         */
        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
